package d0903;
import  java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Demo2 demo = new Demo2();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入用户名: ");
            String username = sc.nextLine();
            System.out.print("请输入密码: ");
            String password = sc.nextLine();
            try {
                boolean bool = demo.login(username, password);
                if (bool) {
                    break;
                }
            } catch (MyException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                // 除了自定义异常, 其他异常都不应该直接显示给用户看
                e.printStackTrace();
                System.out.println("业务繁忙, 请稍后再试!");
            }
        }
        System.out.println("登录成功!");
    }

    public boolean login(String username, String password) throws MyException {

        if (username.isEmpty()) {
            throw new MyException("请输入用户名!");
        }
        if (username.length() < 4) {
            throw new MyException("用户名不能少于4个字符");
        }
        if (password.isEmpty()) {
            throw new MyException("请输入密码!");
        }
        if (password.length() < 6) {
            throw new MyException("密码不能少于6个字符");
        }

        if ("ycyc".equals(username) && "123456".equals(password)) {
            return true;
        } else {
            return false;
        }

    }

}

/**
 *	自定义异常:
 *	1. 序列化版本编号
 *	2. 重写构造函数
 */
class MyException extends Exception {

    /**
     *  序列化版本编号
     */
    private static final long serialVersionUID = 1L;

    public MyException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public MyException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public MyException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public MyException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
}
